package com.aube.plugin.episode;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.aube.plugin.origin.R;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.DynamicLoaderHelper;
import com.huyn.baseframework.dynamicload.IPositionListener;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.Category;
import com.huyn.baseframework.utils.SysUtil;
import com.huyn.baseframework.utils.UmengUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by huyaonan on 16/9/27.
 */
public class HorizontalVideoGroup extends DLLayout implements VideoGroup {

    private List<VideoItem> mAnimItems = new ArrayList<>();
    private AtomicBoolean mAnimated = new AtomicBoolean(false);
    private LinearLayout mVideoLayout;

    private View[] mGuideTarget;

    public HorizontalVideoGroup(Context context) {
        this(context, null);
    }

    public HorizontalVideoGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalVideoGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        inflate(R.layout.video_horizontal, this, true);
        mVideoLayout = (LinearLayout) findViewById(R.id.video_horizontal_group);
    }

    @Override
    public void fillData(Object o) {
    }

    public void initData(final List<VideoModel> list, final Category category, final IUmengListener listener) {
        int width = SysUtil.getAbsoluteScreenWidth(context);
        int space = getResources().getDimensionPixelOffset(R.dimen.widget_margin);
        int size = list.size();
        int itemWidth = size > 2 ? (width-4*space)/2 : (width-3*space)/2;
//        mGuideTarget = new View[2];
        for(int i=0; i<size; i++) {
            final VideoModel item = list.get(i);
            VideoItem mVideo = (VideoItem) DynamicLoaderHelper.loadTargetLayout(context, getDLPluginPackage(), VideoItem.class.getName());
            mVideoLayout.addView(mVideo, new LinearLayout.LayoutParams(itemWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
            mVideo.setupContent(item, itemWidth);

            mVideo.setAlpha(0f);
            mAnimItems.add(mVideo);

//            if(i<2)
//                mGuideTarget[i] = mVideo.getImageView();

            if (item.isLegal()) {
                mVideo.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mBridge != null)
                            mBridge.onClickToVideo(item.toChannelDetail(), category);
                        if (listener != null)
                            listener.reportToUmeng(UmengUtils.appendLabels(item.videoid, item.videoTitle));
                    }
                });
            }

            if(i == list.size()-1)
                continue;
            mVideoLayout.addView(new View(context), new LinearLayout.LayoutParams(space, itemWidth * 3 / 4));
        }
    }

    @Override
    public void notifyAnim(int i) {
        if(mAnimated.get())
            return;
        mAnimated.set(true);
        if(mAnimItems == null)
            return;
        notifyItemAnim(i + 300);
    }

    private void notifyItemAnim(int animDelay) {
        int i=0;
        for(IPositionListener item : mAnimItems) {
            item.notifyAnim(animDelay + i*200);
            i++;
        }
    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }

    @Override
    public View[] getGuideTarget() {
        if(mAnimItems.size() >= 1) {
            mGuideTarget = new View[2];
            mGuideTarget[0] = mAnimItems.get(0).getImageView();
            mGuideTarget[0] = mAnimItems.get(0).getImageView();
            return mGuideTarget;
        }
        return null;
    }
}
